package com.lizhao.win007com.crawler.webapi.controller;

import com.lizhao.win007.com.crawler.service.ICrawlerService;
import com.lizhao.win007com.commons.pojo.info.model.MonitorEasyRealTimeInfo;
import com.lizhao.win007com.commons.restful.JsonResult;
import com.lizhao.win007com.commons.utils.HttpUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "crawler")
@RequestMapping("/crawler")
public class CrawlerController {
    @Autowired
    private ICrawlerService crawlerService;

    // 重新采集某个赛事的盘口数据
    @GetMapping("/update/odds/leagueid/{leagueid:[0-9]+}/pageid/{pageid:[0-9]+}/season/{season:[0-9]+\\-[0-9]+}/" +
            "rounds/{rounds:[0-9]+}")
    public JsonResult<String> updateOddsByLeagueIdAndPageIdAndSeason(@PathVariable Integer leagueid,
                                                                     @PathVariable Integer pageid,
                                                                     @PathVariable String season,
                                                                     @PathVariable Integer rounds
                                                                     ) throws Exception {


        String leagueDir = "D:\\py\\pywin007com\\rounds\\league\\" + leagueid;
        File file = new File(leagueDir);
        if (!file.isDirectory()){
            file.mkdir();
        }

        //更新亚盘
        String url1 = "http://vip.titan007.com/AsianOdds_n.aspx?id=" + pageid;
        String content1 = crawlerService.updateAsianAndBigByUrlAndPageId(url1, pageid);

        String path1 = leagueDir + "\\" + season + "-" + pageid + "-1-round-" + rounds + ".html";
        HttpUtils.makeFile(path1, content1);
        System.out.println("生成亚盘：" + content1);

        //更新大小盘
        String url2 = "https://vip.titan007.com/OverDown_n.aspx?id=" + pageid;
        String content2 = crawlerService.updateAsianAndBigByUrlAndPageId(url2, pageid);

        String path2 = leagueDir + "\\" + season + "-" + pageid + "-2-round-" + rounds + ".html";
        HttpUtils.makeFile(path2, content2);
        System.out.println("生成大小：" + content2);

        //更新欧指
        String url3 = "http://1x2d.titan007.com/" + pageid + ".js";
        String content3 = crawlerService.getOddsByUrl(url3, pageid);

        String path3 = leagueDir + "\\" + season + "-" + pageid + "-3-round-" + rounds + ".html";
        HttpUtils.makeFile(path3, content3);
        System.out.println("生成欧指：" + content3);

        String[] data = {content1, content2, content3};


        return JsonResult.ok("返回盘口内容");
    }

    // 获取实时今日数据
    @GetMapping("/easy/realtime")
    public JsonResult<List<MonitorEasyRealTimeInfo>> getTodayRealTimeData() throws IOException, InterruptedException {
        List<MonitorEasyRealTimeInfo> list = crawlerService.getRealTimeData();
        return JsonResult.ok("获取实时数据", list);
    }
}